%include
:- lib(ic), lib(listut).

testIntegerRangeSum(OState, Arg, Result, NState) :-
	% Domains
	[OStart] :: -127 .. 128,
	[NStart] :: -127 .. 128,
	[OEnd] :: -127 .. 128,
	[NEnd] :: -127 .. 128,
	% Constraints
	OState = [OStart, OEnd],
	NState = [NStart, NEnd],
	integerRangeSum(OState, Arg, Result, NState),
	% Solving
	indomain(OStart),
	indomain(NStart),
	indomain(OEnd),
	indomain(NEnd).

integerRangeSum([OStart, OEnd], [], Result, [NStart, NEnd]) :-
	% Invariant
	OStart #=< OEnd,
	% Postcondition
	Counter0 #= 0,
	indomain(OStart),
	indomain(OEnd),
	(foreach(E1, Coll), count(I, OStart, OEnd) do E1 = I ),
	length(Coll, Limit),
	Answer0 #= 0,
	Counter0 #< Limit,
	nth0(Counter0, Coll, I0),
	Answer1 #= Answer0 + I0,
	Result #= Answer1,
	Counter1 #= Counter0 + 1,
	neg(Counter1 #< Limit),
	% Object State Update
	NStart #= OStart,
	NEnd #= OEnd,
	% Invariant
	NStart #=< NEnd.

